﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static async Task ThrowAfter(int timeout, Exception ex)
        {
            await Task.Delay(timeout);
            throw ex;
        }

        static void PrintException(Exception ex)
        {
            Console.WriteLine("Time: {0}\n{1}\n============", _watch.Elapsed, ex);
        }

        static Stopwatch _watch = new Stopwatch();

        static async Task MissHandling()
        {
            var t1 = ThrowAfter(1000, new NotSupportedException("Error 1"));
            var t2 = ThrowAfter(2000, new NotImplementedException("Error 2"));

            try
            {
                await t1;
            }
            catch (NotSupportedException ex)
            {
                PrintException(ex);
            }
        }

        static async Task BothHandled()
        {
            var t1 = ThrowAfter(1000, new NotSupportedException("Error 1"));
            var t2 = ThrowAfter(2000, new NotImplementedException("Error 2"));

            try
            {
                await Task.WhenAll(t1, t2);
            }
            catch (NotSupportedException ex)
            {
                PrintException(ex);
            }
        }


        static void Main(string[] args)
        {
            _watch.Start();
            BothHandled();
            Console.ReadLine();
        }
    }
}
